<?php
class Default_Model_PtrTinRaoMapper {

	protected $_dbTable;
	protected $db;

	public function setDbTable($dbTable) {
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable('Default_Model_DbTable_PtrTinRao');
		}
		return $this->_dbTable;
	}

	public function getDb(){
		if ($this->db === null) {
			$this->db = Zend_Registry::get('connectDB');
		}
		return $this->db;
	}
	/*
	 * count item
	 */

	public function countItem($arrParam = null, $query = null) {
		$table = $this->getDbTable();
		$select = $table->select();
		$select->from($table, array("count(id) as totalItem"));
		if ($query != null)
        	$select->where($query);
        
		if (isset($arrParam['ssFilter'])) {
			$ssFilter = $arrParam['ssFilter'];
			if (!empty($ssFilter['keywords'])) {
				$keywords = $ssFilter['keywords'];
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('(tieu_de LIKE ? OR mo_ta LIKE ? OR dia_chi LIKE ?)', $keywords);
			}
	
			if (!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
				$col = $ssFilter['col'];
				$order = $ssFilter['order'];
				$select->order(array($col . ' ' . $order));
			}
        }
        
		$resultSet = $table->fetchAll($select);
		$array = $resultSet->toArray();
		return $array[0]['totalItem'];
	}
	
	/*
	 * fetch
	 */
	public function fetch($arrParam = null, $query = null, $num_new = 0, $colOrder = array(array('colName' => 'ngay_dang', 'order' => 'DESC')), $option = 'view') {
		$paginator = $arrParam['paginator'];
		$keywords = '';
		
		$table = $this->getDbTable();
        $select = $table->select();
        $select->from($table);
        
        if ($query != null)
        	$select->where($query);

        if (isset($arrParam['ssFilter'])) {
			$ssFilter = $arrParam['ssFilter'];
			if (!empty($ssFilter['keywords'])) {
				$keywords = $ssFilter['keywords'];
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('(tieu_de LIKE ? OR mo_ta LIKE ? OR dia_chi LIKE ?)', $keywords);
			}
	
			if (!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
				$col = $ssFilter['col'];
				$order = $ssFilter['order'];
				$select->order(array($col . ' ' . $order));
			}
        }
		
		if ($num_new > 0) {
			$select->limitPage(0, $num_new);
		} elseif ($paginator['itemCountPerPage'] > 0) {
			$page = $paginator['currentPage'];
			$rowCount = $paginator['itemCountPerPage'];
			$select->limitPage($page, $rowCount);
		}
		
		$order = array();
		foreach ($colOrder as $val) {
			array_push($order, $val['colName'] . ' ' . $val['order']);
		}
		$select->order($order);
		
		$resultSet = $this->getDbTable()->fetchAll($select);
		return $this->processResultSet($resultSet->toArray(), $option);

	}
	
	/*
	 * processResultSet
	 */
	private function processResultSet($resultSet, $option = 'view') {
		$entries = "";
		if($option == 'view') {
			foreach ($resultSet as $row) {
				$entries = new Default_Model_TableColumn_PtrTinRao($row);
			}
		} else {
			$entries = array();
			foreach ($resultSet as $row) {
				$entry = new Default_Model_TableColumn_PtrTinRao($row);
				$entries[] = $entry;
			}
		}
		return $entries;
	}
	
	/*
	 * save item
	 */
	public function save(Default_Model_TableColumn_PtrTinRao $ptrtinrao) {
		$date_format = Default_Model_TableColumn_PtrTinRao::DATE_FORMAT_FULL_EN;
		$now = date($date_format, time());
		$data = array(
			'id' 				=> $ptrtinrao->getId(),
			'tieu_de' 			=> $ptrtinrao->getTieu_de(),
			'mo_ta' 			=> $ptrtinrao->getMo_ta(),
			'loai_tin' 			=> $ptrtinrao->getLoai_tin(),
			'dia_chi' 			=> $ptrtinrao->getDia_chi(),
			'video_url' 		=> $ptrtinrao->getVideo_url(),
			'hinh_anh' 			=> $ptrtinrao->getHinh_anh(),
			'thanh_pho' 		=> $ptrtinrao->getThanh_pho(),
			'quan_huyen' 		=> $ptrtinrao->getQuan_huyen(),
			'goi_tin' 			=> $ptrtinrao->getGoi_tin(),
			'loai_phong' 		=> $ptrtinrao->getLoai_phong(),
			'so_nguoi_o' 		=> $ptrtinrao->getSo_nguoi_o(),
			'dien_tich' 		=> $ptrtinrao->getDien_tich(),
			'gia_phong' 		=> $ptrtinrao->getGia_phong(),
			'gia_dien' 			=> $ptrtinrao->getGia_dien(),
			'gia_nuoc' 			=> $ptrtinrao->getGia_nuoc(),
			'nguoi_lien_he' 	=> $ptrtinrao->getNguoi_lien_he(),
			'so_dien_thoai'		=> $ptrtinrao->getSo_dien_thoai(),
			'latitude' 			=> $ptrtinrao->getLatitude(),
			'longitude' 		=> $ptrtinrao->getLongitude(),
			'nguoi_dang'		=> $ptrtinrao->getNguoi_dang(),
		);
		
		if (0 == ($id = $ptrtinrao->getId())) {
			unset($data['id']);
			$data['ngay_dang'] = $now;
			$this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id = ?' => $id));
		}
	}
	
	/*
	 * change item
	 */
	public function change($id, $data = null) {
		$where = 'id IN (' . $id . ')';
		$this->getDbTable()->update($data, $where);
	}

	/*
	 * delete function
	 */
	public function delete($id = 0, $key = 'id') {
		$where = $key . ' IN (' . $id . ')';
		$this->getDbTable()->delete($where);
	}
}